import React, { useState } from 'react'
import styles from './index.less'
import { config } from '@/config'
import { useModel } from 'umi'
import { Button, message } from 'antd'
import { useMount } from '@umijs/hooks'
import { HttpQueryVersion } from '@/services'

const Ver: React.FC<{}> = () => {
    const [version, setVersion] = useState<Record<string, any>>({})
    useMount(async () => {
        const { flag, data } = await HttpQueryVersion()
        if (flag === 1) {
            setVersion(data)
        }
    })
    const download = (url: string) => {
        if (!url) {
            alert('系统错误，请联系管理员！')
            return
        }
        if (window.$bty) {
            window.$bty?.jsGlobal?.send(window.$bty.IpcEvent.OPEN_URL, url)
            window.$bty?.jsGlobal?.quit()
        } else {
            open(url)
        }
    }
    const osType = (v: Record<string, any>) => {
        const agent = navigator.userAgent.toLowerCase()
        const isMac = /macintosh|mac os x/i.test(navigator.userAgent)
        if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
            download(v.win)
        }
        if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
            download(v.win)
        }
        if (isMac) {
            download(v.mac)
        }
    }
    return (
        <div className={styles.container}>
            <div className={styles.content}>
                <div className={styles.top}>
                    <div className={styles.header}>
                        <span className={styles.title}>您的版本过旧，请下载新版本</span>
                    </div>
                    <div className={styles.desc}>
                        当前版本：v{config.version}，最新版本：v{version.version}
                    </div>
                </div>
                <div className={styles.main} style={{ textAlign: 'center' }}>
                    <Button
                        type='primary'
                        onClick={async () => {
                            osType(version)
                        }}>
                        下载
                    </Button>
                </div>
            </div>
        </div>
    )
}
export default Ver
